home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 331_01 / ged5.c < prev    next >
Text File  |  1990-06-12  |  7KB  |  311 lines

  1. /*
  2. HEADER:         CUG999.05;
  3. TITLE:          GED (nee QED) screen editor -- part 5;
  4. DATE:           10/10/86;
  5.  
  6. DESCRIPTION:    "File manipulation commands for the GED editor.";
  7. FILENAME:       GED5.C;
  8. AUTHORS:        G. Nigel Gilbert, James W. Haefner, Mel Tearle, G. Osborn;
  9. COMPILERS:      Microsoft 4.0;
  10. */
  11.  
  12. /*
  13.      e/qed/ged  screen editor
  14.  
  15.     (C) G. Nigel Gilbert, MICROLOGY, 1981
  16.            August-December 1981
  17.  
  18.     Modified:  Aug-Dec   1984:   BDS-C 'e'(vers 4.6a) to 'qe' (J.W. Haefner)
  19.                March     1985:   BDS-C 'qe' to DeSmet-C 'qed' (J.W. Haefner)
  20.                May       1986:   converted to ged - Mel Tearle
  21.  
  22.     FILE:      ged5.c
  23.  
  24.     FUNCTIONS: readfile, writefile, exists, checkexists, scans, retag,
  25.                format
  26.  
  27.     PURPOSE:   read and write files
  28.  
  29. */
  30.  
  31. #include <stdio.h>
  32. #include "ged.h"
  33.  
  34. #define  GS   0x1d
  35. #define  ESC  0x1b
  36.  
  37.  
  38. /* read text into current file from 'filename' with ^KR
  39.  * does not inject into middle of line.  first inserted line
  40.  * is above current line, the same as the line pop operation.
  41.  */
  42. readfile(name)
  43. char *name;
  44. {
  45.     int  c, i, j, k, line, res, cnt;
  46.     char textb[LLIM];
  47.     struct iobuffer *textbuf;
  48.     int fopen1();
  49.  
  50.     if ( fopen1( name, fbuf) == FAIL )  {
  51.         if ( fopen1( strcat( name, defext ), fbuf) == FAIL )  {
  52.             error( " Can't find file " );
  53.             name[0] = '\0';
  54.             return( FAIL );
  55.         }
  56.     }
  57.  
  58.     line  = cline-1;
  59.     do  {
  60.         i=0;
  61.         while ( i < LLIM  &&  ( (c = egetc(fbuf)) != DFAIL )  &&  (c != '\n')
  62.             &&  ( c != ENDFILE && c != ENDCHAR) )
  63.             textb[i++] = c;
  64.  
  65.         if ( textb[i-1] == '\r' )
  66.             i--;
  67.         if ( textb[i-1] == '\n' )
  68.             i--;
  69.         if ( textb[i-1] == '\r' )
  70.             i--;
  71.         if ( textb[i-1] == '\n' )
  72.             i--;
  73.         textb[i] = '\0';
  74.         res = inject( line++, textb );
  75.     }
  76.     while ( c != DFAIL  &&  res  != FAIL  &&  c != ENDFILE );
  77.  
  78.     fclose( fbuf );
  79.  
  80.     blocking = YES;
  81.     vbord1 = cline;
  82.     vbord2 = line;
  83.     if (vbord2 >= plast)
  84.         cursory += plast - vbord2 -1;  /* limit checked in calp() */
  85.     plast = -1;
  86.     moveline(0);
  87.     while (chkbuf() == 0)
  88.         ;
  89.     blocking = NO;
  90.     moveline(0);
  91.     return;
  92. }
  93.  
  94.  
  95. writefile(from,to,name,nametoprint)
  96. int  from, to;
  97. char *name, *nametoprint;
  98. {
  99.     char buf[90];
  100.     int  l;
  101.     char c, *t, *getline();
  102.  
  103.     puttext();
  104.     if ( name[0] <= ' ' )  {
  105.         error(" Bad name ");
  106.         return  FAIL;
  107.     }
  108. /* fcreat is not a library function */
  109.     if ( fcreat( name, fbuf ) == FAIL )  {
  110.         strcpy(buf,"Can't create file ");
  111.         strcat(buf,name);
  112.         error(buf);
  113.         return(FAIL);
  114.     }
  115.     strcpy(buf,"|S|aving: ");
  116.     strcat(buf, nametoprint );
  117.     putmess(buf);
  118.  
  119.     for ( l = from; l <= to; )  {
  120.         t = getline( l++ );
  121.         while ( *t )
  122.             if ( eputc( *t++, fbuf ) == FAIL )
  123.                 goto diskfull;
  124.         if ( eputc( '\r', fbuf ) == FAIL )
  125.             goto diskfull;
  126.         if ( eputc( '\n', fbuf ) == FAIL )
  127.             goto diskfull;
  128.     }
  129.     if (eofchar)
  130.         if ( eputc( ENDCHAR, fbuf ) == FAIL )
  131.             goto  diskfull;
  132.     if ( dflush( fbuf ) == FAIL )
  133.         goto  diskfull;
  134.     if ( fclose( fbuf ) == FAIL )  {
  135.         error( " Can't close file " );
  136.         return FAIL;
  137.     }
  138.     return  YES;
  139.  
  140. diskfull:
  141.     error(" Disk full ");
  142.     return FAIL;
  143. }
  144.  
  145.  
  146. exists(name)
  147. char *name;
  148. {
  149.     char c;
  150.     int buf[81];
  151.  
  152.     c = 'y';
  153.     if ( checkexists( name ) )  {
  154.         strcpy(buf, " OK to replace ");
  155.         strcat(buf, name);
  156.         strcat(buf, " ? ");
  157.         putmess(buf);
  158.         putch( (c = getlow()) );
  159.         putret();
  160.     }
  161.     return c == 'y';
  162. }
  163.  
  164.  
  165. /* return YES if file 'name' exists, else NO
  166.  */
  167. checkexists(name)
  168. char *name;
  169. {
  170.     int  fd;
  171.  
  172. /* if (dskcheck(setjmp(dskerr)) != 0 || (fd=open(name,0)) == FAIL) return NO; */
  173. /* no setjmp just yet */
  174.     if ( ( fd = open( name, 0 ) ) == FAIL )
  175.         return  NO;
  176.     close( fd );
  177.     return  YES;
  178. }
  179.  
  180.  
  181. /* used to input filename, etc in message routine
  182.  */
  183. scans(answer,maxlen)
  184. char *answer;
  185. int maxlen;
  186. {
  187.     unsigned char c;
  188.     int  n, i;
  189.  
  190.     maxlen--;
  191.     n = 0;
  192.     while ( n < maxlen )  {
  193.         c = getscankey();
  194.         switch(c) {
  195.         case F3KEY :
  196.         case F4KEY :
  197.         case F2KEY :
  198.             n = maxlen;
  199.             break;  /* allows use of F3, F4, & F2 as cr for string search terminator*/
  200.         case F1KEY :
  201.         case F6KEY :
  202.         case F5KEY :
  203.         case F7KEY :
  204.         case F8KEY :
  205.         case F9KEY :
  206.         case F10KEY :
  207.             return  ESCKEY;  /* purpose unknown.  g.o. */
  208.         case BACKSP :
  209.         case DELLEFT_P :
  210.             if ( n )  {
  211.                 putch(BACKSP);
  212.                 putch(' ');
  213.                 putch(BACKSP);
  214.                 n--;
  215.                 answer--;
  216.             }
  217.             break;
  218.         case CR_P :
  219.         case ESCKEY_P :
  220.             n = maxlen;
  221.             break;
  222.         case LEFTKEY_P :   /* pass this key code thru */
  223.             c = tran[LEFTKEY];
  224.             goto passthru;
  225.         case RETRIEVE_P :
  226. /* if (n == 0) { */
  227.             if ( *answer )  {
  228.                 while ( *answer )  {
  229.                     dispch( *answer++ );
  230.                     n++;
  231.                 }
  232.                 break;
  233.             }
  234.             else  {
  235.                 c = tran[RETRIEVE]; /*if no answer, display the key code*/
  236.                 goto passthru;
  237.             }
  238.         case GS :           /* kludge to permit ESC in find/alter */
  239.             c = ESC;
  240.         default :
  241. passthru:
  242.             dispch(c);
  243.             *answer++ = c;
  244.             n++;
  245.             break;
  246.         }
  247.     }
  248.     *answer = '\0';
  249. /* this is a work-around until type integer characters are used */
  250.     if (c != F3KEY && c != F4KEY && c != F2KEY)
  251.         c = c & ~PARBIT;
  252.     putret();                   /* set PARBIT of c back to 0 to */
  253.     return (c);
  254. }
  255.  
  256.  
  257. /* puts a new suffix on a file name.  the filespec  ..\name.ext is acceptable
  258.  * in this version.
  259.  */
  260. retag(name,tag)
  261. char *name, *tag;
  262. {
  263.     char *namep, *name0;
  264.  
  265.     name0 = name;
  266.     namep = name;
  267.     while (*name) {
  268.         if (*name == '.')
  269.             namep = name;
  270.          name++;
  271.     }
  272.     if (namep == name0) {
  273.         namep = name;
  274.         *namep = '.';
  275.     }
  276.     strcpy(namep+1,tag);
  277. }
  278.  
  279.  
  280. /* format filename for display */
  281. format(name)
  282. char *name;
  283. {
  284.     char tempname[FILELEN], *n, *tn;
  285.  
  286.     if ( !*name )
  287.        return;
  288.  
  289.     if ( *(name+1) == ':' ) {
  290.         strcpy( tempname, name );
  291.     }
  292.     else  {
  293.         tempname[0] = curdsk + 'A';
  294.         tempname[1] = ':';
  295.         name[FILELEN-1] = '\0';
  296.         strcpy( &tempname[2], name );
  297.     }
  298.     for ( n = name, tn = tempname; ( *n = toupper( *tn++ ) ); n++ )
  299.         if ( *n == '.' )
  300.             break;
  301.     while ( *n++ )
  302.         *n = tolower( *tn++ );
  303.     return;
  304. }
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.